package com.daocao.sysuser.controller;

import com.daocao.common.domain.entity.UmsMenu;
import com.daocao.common.domain.vo.RouterVO;
import com.daocao.common.response.DaoCaoPageResult;
import com.daocao.common.response.DaoCaoResult;
import com.daocao.common.service.IUmsMenuService;
import com.daocao.common.domain.dto.UmsMenuParamDto;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @date 石添
 * @date 2023/12/14
 */
@RestController
@RequestMapping("sys/menu")
public class MenuController {

    private final IUmsMenuService menuService;

    public MenuController(IUmsMenuService menuService) {
        this.menuService = menuService;
    }

    @PostMapping
    public DaoCaoResult saveMenu(@Valid @RequestBody UmsMenu umsMenu) {
        int row = menuService.saveMenu(umsMenu);
        if(row > 0) {
            return DaoCaoResult.success();
        }
        return DaoCaoResult.error("新增菜单失败");
    }

    @PutMapping
    public DaoCaoResult updateMenu(@Valid @RequestBody UmsMenu umsMenu) {
        int row = menuService.updateMenu(umsMenu);
        if(row > 0) {
            return DaoCaoResult.success();
        }
        return DaoCaoResult.error("修改菜单失败");
    }

    /**
     * 查询自己的菜单
     */
    @GetMapping("self")
    public DaoCaoResult searchSelfMenu() {
        // 获取当前登录的用户id,都在SecurityContextHolder中存储
        List<RouterVO> routerVOList = menuService.searchSelfMenu();
        return DaoCaoResult.success(routerVOList);
    }

    /**
     * 查询所有的菜单
     */
    @GetMapping("list")
    public DaoCaoResult list(UmsMenuParamDto menuParamDto) {
        DaoCaoPageResult<UmsMenu> pageResult = menuService.selectList(menuParamDto);
        return DaoCaoResult.success(pageResult);
    }

    /**
     * 根据id查询详情
     * @return
     */
    @GetMapping("{id}")
    public DaoCaoResult searchInfo(@PathVariable("id") Long id) {
        UmsMenu menu = menuService.searchInfo(id);
        return DaoCaoResult.success(menu);
    }

    /**
     * 删除数据
     * @param ids
     * @return
     */
    @DeleteMapping
    public DaoCaoResult removeMenu(@RequestBody Long[] ids) {
        int row = menuService.removeMenu(ids);
        if(row > 0) {
            return DaoCaoResult.success();
        }
        return DaoCaoResult.error();
    }

}
